<template>
    <div>
        <div style="margin-bottom: 20px;">
            <el-input 
                    style="width: 200px;"
                    size="small"
                    placeholder="添加职称..."
                    suffix-icon="el-icon-plus"
                    v-model="jobLevel.name">
            </el-input>
            <el-select v-model="jobLevel.titleLevel" placeholder="选择职称等级" size="small" style="width: 130px;margin: 0 5px;">
                <el-option v-for='item in titleLevels' :key="item" :label="item" :value="item"></el-option>
            </el-select>

            <el-button size="small" icon="el-icon-plus" type="primary" @click="addJobLevel">添加职称</el-button>
        </div>
        <div style="margin-bottom: 10px;">
            <el-table
                    :data="jobLevels"
                    size="small"
                    stripe
                    border
                    @selection-change="handleSelectionChange"
                    style="width: 70%">
                <el-table-column type="selection" width="55"></el-table-column>
                <el-table-column prop="id" label="编号" width="55"></el-table-column>
                <el-table-column prop="name" label="职称" width="120"></el-table-column>
                <el-table-column prop="titleLevel" label="职称等级" width="120"></el-table-column>
                <el-table-column prop="createDate" label="创建时间" width="200"></el-table-column>
                <el-table-column prop="enabled" label="是否启用" width="80">
                    <template slot-scope="scope">
                        <!--JSON.parse(scope.row.enabled)  将字符串转为boolean值-->
                        <el-tag v-if="JSON.parse(scope.row.enabled)" type="success">已启用</el-tag>
                        <el-tag v-else type="danger">未启用</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="编号">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                @click="showEditView(scope.$index,scope.row)">编辑
                        </el-button>
                        <el-button
                                size="mini"
                                type="danger"
                                @click="handleDelete(scope.$index,scope.row)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-button type="danger" size="small" :disabled="this.multipleSelection.length === 0" @click="deleteMany">批量删除
        </el-button>
        <el-dialog 
                title="编辑职称"
                :visible.sync="dialogVisible"
                width="30%">
            <div>
                <div style="margin-bottom:5px;margin-left:8px">
                    <el-tag>职称名称</el-tag>
                </div>
                <div style="margin-bottom:5px">
                    <el-input v-model="updateJobLevel.name" size="small" style="margin-left: 8px;"></el-input>
                </div>
                <div style="margin-bottom:5px;margin-left:8px;margin-right:5px">
                    <el-tag>职称等级</el-tag>
                    <span style="margin-left:8px">
                        <el-select v-model="updateJobLevel.titleLevel" placeholder="选择职称级别" size="small" style="width :130px">
                            <el-option v-for="item in titleLevels" :key="item" :label="item" :value="item">{{item}}</el-option>
                        </el-select>
                    </span>
                </div>
                <div style="margin-left:8px">
                <el-tag>是否启用</el-tag>
                    <el-switch v-model="updateJobLevel.enabled"
                                    active-color="#13ce66"
                                    inactive-color="#ff4949"
                                    active-text="启用"
                                    inactive-text="禁用"
                                    style="margin-left: 10px;">
                    </el-switch>
                </div>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button size="small" @click="dislogVisible = false">取 消</el-button>
                <el-button size="small" type="primary" @click="doUpdateJob">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name:"JoblevelMana",
    data(){
        return{
           // 添加信息
           jobLevel:{
               name: '',
               titleLevel: ''
           },
           // 职称等级
           titleLevels:['正高级','副高级','中级','初级','员级'],
           // 所有信息
           jobLevels:[],
           // 编辑框
           dialogVisible: false,
           // 更新信息
           updateJobLevel:{
               name:'',
               titleLevel:'',
               enabled:'',
           },
           // 多选
           multipleSelection:[],
        }
    },
    methods:{
        // 获取所有职称
        initJobLevels() {
            this.getRequest('/system/basic/joblevel/').then(resp => {
                if (resp) {
                    this.jobLevels = resp;
                }
            })
        },

        // 添加职称
        addJobLevel(){
            if (this.jobLevel.name && this.jobLevel.titleLevel ){
                this.postRequest('/system/basic/joblevel/',this.jobLevel).then(resp =>{
                    if (resp){
                        this.initJobLevels();
                        // 清空输入框的信息
                        this.jobLevel.name = '';
                        this.jobLevel.titleLevel = '';
                    }
                })
            } else {
                this.$message.error("添加信息不能为空！");
            }
        },
        // 显示编辑界面
        showEditView(index,data){
            Object.assign(this.updateJobLevel,data);
            this.updateJobLevel.createDate = '';
            this.updateJobLevel.enabled = JSON.parse(data.enabled);
            this.dialogVisible = true;
        },
        // 更新信息
        doUpdateJob(){
            this.dialogVisible = false;
            this.putRequest('/system/basic/joblevel/',this.updateJobLevel).then(resp => {
                if (resp){
                    // 重新查询所有信息
                    this.initJobLevels();
                    // 关闭编辑界面
                    this.dialogVisible = false;
                }
            })
        },
        // 删除职称
        handleDelete(index,data){
            // 提示框
            this.$confirm('此操作将永久删除职称['+data.name+']，是否继续？','提示',{
                confirmButtonText:'确定',
                cancelButtonText:'取消',
                type :'warning'
            }).then(() => {
                this.deleteRequest('/system/basic/joblevel/'+data.id).then(resp =>{
                    if (resp){
                        this.initJobLevels();
                    }
                })
            }).catch(() => {
                this.$message({
                    type: "info",
                    message: "已取消操作"
                })
            })
        },
        // 多选
        handleSelectionChange(rows){
            // 将选择的值赋值给multipleSelection
            this.multipleSelection = rows;
        },

        // 批量删除
        deleteMany(){
            this.$confirm('此操作将永久删除['+this.multipleSelection.length+']个职称，是否继续？','提示',{
                confirmButtonText:'确定',
                cancelButtonText:'取消',
                type :'warning'
            }).then(() => {
                let ids = '?';
                this.multipleSelection.forEach(item => {
                    ids += 'ids' + item.id + '&';
                })
                this.deleteRequest('/system/basic/joblevel'+ ids).then(resp =>{
                    if (resp){
                        this.initJobLevels();
                    }
                })
            }).catch(() => {
                this.$message({
                    type: "info",
                    message: "已取消操作"
                })
            })
        }
    },
    mounted(){
        this.initJobLevels();
    }
}
</script>

<style>

</style>